<?php

/**
 * This is the model class for table "users".
 *
 * The followings are the available columns in table 'users':
 * @property integer $user_id
 * @property string $username
 * @property string $password
 * @property string $salt
 * @property string $email
 * @property integer $status
 * @property integer $seeking
 * @property string $avatar
 * @property string $verify_code
 * @property string $created_at
 * @property string $last_login_time
 * @property string $last_login_ip
 * @property integer $is_online
 */
class FormLogin extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return FormLogin the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'users';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('username, password, email', 'required'),
			array('status, seeking, is_online', 'numerical', 'integerOnly'=>true),
			array('username, avatar, verify_code', 'length', 'max'=>255),
			array('password', 'length', 'max'=>32),
			array('salt, email, last_login_ip', 'length', 'max'=>128),
			array('created_at, last_login_time', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('user_id, username, password, salt, email, status, seeking, avatar, verify_code, created_at, last_login_time, last_login_ip, is_online', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}
        
        public function authenticate($attribute,$params)
	{
		$this->_identity=new UserIdentity($this->username,$this->password);
                $this->_identity=new UserIdentity($this->email,$this->password);
		if(!$this->_identity->authenticate())
			$this->addError('password','Incorrect username,email or password.');
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'user_id' => 'User',
			'username' => 'Username',
			'password' => 'Password',
			'salt' => 'Salt',
			'email' => 'Email',
			'status' => 'Status',
			'seeking' => 'Seeking',
			'avatar' => 'Avatar',
			'verify_code' => 'Verify Code',
			'created_at' => 'Created At',
			'last_login_time' => 'Last Login Time',
			'last_login_ip' => 'Last Login Ip',
			'is_online' => 'Is Online',
		);
                
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('salt',$this->salt,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('seeking',$this->seeking);
		$criteria->compare('avatar',$this->avatar,true);
		$criteria->compare('verify_code',$this->verify_code,true);
		$criteria->compare('created_at',$this->created_at,true);
		$criteria->compare('last_login_time',$this->last_login_time,true);
		$criteria->compare('last_login_ip',$this->last_login_ip,true);
		$criteria->compare('is_online',$this->is_online);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        
        public function login()
	{
		if($this->_identity===null)
		{
			$this->_identity=new UserIdentity($this->username,$this->password);
                        $this->_identity=new UserIdentity($this->email,$this->password);
			$this->_identity->authenticate();
		}
		if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
		{
			$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
			Yii::app()->user->login($this->_identity,$duration);
			return true;
		}
		else
			return false;
	}
}